package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzMortgageMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.dz.IDzMortgageService;
import com.zjdiepu.www.model.dz.dto.DzMortgage;
import com.zjdiepu.www.model.dz.ext.DzMortgageExt;

/**
 * 车辆抵押确认Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 16时17分<br/>
 * @since 1.0<br/>
 */
@Service
public class DzMortgageServiceImpl extends BaseService implements IDzMortgageService {
	public static final Logger logger = LoggerFactory.getLogger(DzMortgageServiceImpl.class);

	@Autowired
	private DzMortgageMapper dzMortgageMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Override
	public ApiResponse saveMortgageRegistration(DzMortgage dzMortgage, AbstractSysUser currentUser) {
		
		//zhangpp 2017-06-30
		//	if(!dzMortgage.isCompleted()) {
		//		return ApiResponse.fail(901, "抵押确认信息不完整");
		//	}
		
		// 影响的记录数
		dzMortgage.initInsertData(currentUser.getId(), currentUser.getName());
		dzMortgageMapper.insert(dzMortgage);
	    
//		YwBusiness ywBusiness = new YwBusiness();
//		ywBusiness.setId(dzMortgage.getBusiId());
//		ywBusiness.setBusiStatus(BusiStatus.CHE_LIANG_YI_DI_YA.getCode());
//		ywBusiness.setPledgeTime(dzMortgage.getTimeMortgage());
//		ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		return ApiResponse.success(dzMortgage.getId());
	}
	
	@Override
	public void saveUpdateAndDelete(DzMortgage dzMortgage, OperateType operType) {
		switch (operType) {
		case add:
			dzMortgage.initInsertData(dzMortgage.getModifier(), dzMortgage.getModifierName());
			dzMortgageMapper.insert(dzMortgage);
			break;
		case edit:
			dzMortgage.initUpdateData(dzMortgage.getModifier(), dzMortgage.getModifierName());
			// 影响的记录数
			int updateCount = dzMortgageMapper.updateByPrimaryKeySelective(dzMortgage);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzMortgage.initDeleteData(dzMortgage.getModifier(), dzMortgage.getModifierName());
			// 影响的记录数
			int deleteCount = dzMortgageMapper.updateByPrimaryKeySelective(dzMortgage);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzMortgage> selectByExt(DzMortgageExt dzMortgageExt) {
		return dzMortgageMapper.selectByExt(dzMortgageExt);
	}
	
	@Override
	public int countByExt(DzMortgageExt dzMortgageExt){
		return dzMortgageMapper.countByExt(dzMortgageExt);
	}
	
	@Override
	public DzMortgage selectById(String id) {
		return dzMortgageMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzMortgageMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzMortgage dzMortgage) {
		return dzMortgageMapper.countRefined(dzMortgage);
	}

	@Override
	public DzMortgage selectRefined(DzMortgage dzMortgage) {
		return dzMortgageMapper.selectRefined(dzMortgage);
	}
	
	@Override
	public int deletedRefined(DzMortgage dzMortgage) {
		return dzMortgageMapper.deletedRefined(dzMortgage);
	}

	@Override
	public boolean repeat(DzMortgage dzMortgage) {
		int count = countRefined(dzMortgage);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzMortgage.getId())) {
			return true;
		}
		DzMortgage dbItem = selectRefined(dzMortgage);
		if(null == dbItem) {
			return false;
		}
		return !dzMortgage.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzMortgageMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DzMortgage> dzMortgages) {
		return dzMortgageMapper.insertBatch(dzMortgages);
	}
}
